package com.study.dialoglib;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity;

import java.lang.ref.WeakReference;

/**
 * Create： junfei_zhang 10/31/20 8:29 PM
 * 描述：提示框
 */
public class CommonDialog extends DialogFragment {
    private final String TAG = getClass().getName();
    /**
     * 属性值
     */
    private String title;
    private String content;
    private String cancel;
    private String confirm;
    private OnBtnClickListener clickListener;

    //activity的弱引用
    private WeakReference<FragmentActivity> contextWeakReference;

    public enum CLICK_TYPE {
        CANCEL, CONFIRM
    }


    private CommonDialog() {
    }

    public static class Builder {
        private String title = "标题";
        private String content = "提示内容";
        private String cancel = "取消";
        private String confirm = "确定";
        private OnBtnClickListener clickListener;

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setContent(String content) {
            this.content = content;
            return this;
        }

        public Builder setCancelBtn(String cancel) {
            this.cancel = cancel;
            return this;
        }

        public Builder setConfirmBtn(String confirm) {
            this.confirm = confirm;
            return this;
        }

        public Builder setOnBtnClickListener(OnBtnClickListener clickListener) {
            this.clickListener = clickListener;
            return this;
        }

        public CommonDialog build(FragmentActivity context) {
            CommonDialog dialog = new CommonDialog();
            dialog.contextWeakReference = new WeakReference<>(context);
            dialog.title = this.title;
            dialog.content = this.content;
            dialog.cancel = this.cancel;
            dialog.confirm = this.confirm;
            dialog.clickListener = this.clickListener;
            return dialog;
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

        return initDialog();
    }

    /**
     * 构造提示框
     */
    public Dialog initDialog() {
        return new AlertDialog
                .Builder(contextWeakReference.get())
                .setTitle(title)
                .setMessage(content)
                .setNegativeButton(cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (CommonDialog.this.clickListener != null) {
                            CommonDialog.this.clickListener.onClick(dialog, CLICK_TYPE.CANCEL, "取消");
                        } else {
                            dialog.cancel();
                        }
                    }
                })
                .setPositiveButton(confirm, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (CommonDialog.this.clickListener != null) {
                            CommonDialog.this.clickListener.onClick(dialog, CLICK_TYPE.CONFIRM, content);
                        } else {
                            dialog.cancel();
                        }
                    }
                })
                .create();
    }

    /**
     * 自定义弹框layout
     */
    public View initDialogView() {
        return null;
    }

    public void show() {
        if (contextWeakReference.get() == null || contextWeakReference.get().isDestroyed()) {
            Log.e(TAG, "Context可能指向了一个被销毁的activity");
            return;
        }
        this.show(contextWeakReference.get().getSupportFragmentManager(), "CommonDialog");
    }

    public interface OnBtnClickListener {
        void onClick(DialogInterface dialog, CLICK_TYPE type, String msg);
    }
}
